package dataStructure.stack;

public class Calculator {
    public static void main(String[] args) {
        ArrayToStackP numsStack = new ArrayToStackP(20);
        ArrayToStackP operStack = new ArrayToStackP(20);
        String key = "2*3/6+2*3";
        int res=0;
        int num1=0;
        int num2=0;
        int oper=0;
        int index = 0;
        char ch = ' '; //将每次扫描得到 char 保存到 ch
        String s = "";

        while (true){
             ch = key.substring(index, index + 1).charAt(0);

            //判断是否是运算符
            if (operStack.isOper(ch)){
                //判断运算符栈是否为空
                if (!operStack.isnull()){
                    //判断进栈的运算符是否比栈顶的运算符大
                    if (operStack.priority(ch)<=operStack.priority(operStack.getHead())){
                        //让数栈弹出两个来做栈顶运算
                        num1 = numsStack.pop();
                        num2 = numsStack.pop();
                        oper = operStack.pop();
                       res =  numsStack.cal(num1,num2,oper);
                       numsStack.push(res);
                       operStack.push(ch);
                    }else {
                        operStack.push(ch);
                    }

                }else {
                    operStack.push(ch);
                }

            }else {
                //多位数处理
                if (index==key.length()-1){
                    numsStack.push(ch-48);
                }else {
                    s +=ch;
                    if (operStack.isOper(key.substring(index+1,index+2).charAt(0))){
                        numsStack.push(Integer.parseInt(s));
                        s = "";
                    }
                }

            }
            index++;
            if (index>=key.length()){
                break;
            }
        }
        while (true){
            if (operStack.isnull()){
                break;
            }
            num1 = numsStack.pop();
            num2 = numsStack.pop();
            oper = operStack.pop();
            res =  numsStack.cal(num1,num2,oper);
            numsStack.push(res);
        }
        System.out.printf("result: %d",numsStack.pop());

    }
}

class ArrayToStackP{
    private int sizeMax;
    public int top;
    public int[] data;
    ArrayToStackP(int sizeMax){
        this.sizeMax = sizeMax;
        data = new int[this.sizeMax];
        top = -1;
    }

    public void push(int x){
        if (!isfull()){
            top++;
            data[top]=x;
        }

    }
    public int pop(){
        if (!isnull()){
            int value = data[top];
            top--;
            return value;
        }
        return -1;

    }
    public void print(){
        //从栈顶遍历
        for (int i =top;i>-1;i--){
            System.out.println(data[i]);
        }
    }

    //取栈顶
    public int getHead(){
        return data[top];
    }

    //判断是否是运算符
    public boolean isOper(char key){
        switch (key){
            case '+':
            case '-':
            case '*':
            case '/':return true;
            default: return false;
        }

    }

    //判断运算符的优先级
    // char类型底层也是int类型
    public int priority(int key){
        if (key == '+' || key == '-'){
            return 0;
        }else if (key =='*'|| key =='/'){
            return 1;
        }else {
            return -1;
        }
    }

    //计算方法
    public int cal(int num1,int num2,int oper){
        int res = 0;
        switch (oper){
            case '+' : res = num2+num1;break;
            case '-' : res = num2-num1;break;
            case '*' : res = num2*num1;break;
            case '/' : res = num2/num1;break;
            default: break;
        }
        return res;

    }

    public boolean isfull(){
        return top==sizeMax-1;
    }
    public boolean isnull(){
        return top==-1;
    }
}
